﻿using System;
using System.Web.Mail;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using MLY;
using MLY.Dal;
using MLY.Bll;
using System.IO;

namespace MLY.Reports.SCCoverReports
{
    public partial class DeadInventoryReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bool isClient;
                if ((Request.QueryString["userRole"] != null) && (Request.QueryString["userRole"] == "Client"))
                    isClient = true;
                else
                    isClient = false;
                
                IsClientHiddenField.Value = isClient ? "1" : "0";
                
                ddlOrgs_Load(isClient);


            }
        }

        protected void GenerateReportClick(object sender, EventArgs e)
        {
            GenerateReport();
        }

        private void GenerateReport()
        {
            
            // can assume that the txtFromDate includes date as it is must and read only
            string SendDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate.Text.Trim()));
            
            string CustomerWON = CustomerWONTextBox.Text.Trim();
            string ContractorWON = ContractorWONTextBox.Text.Trim();

            ReportDataSource.SelectParameters.Clear();

            ReportDataSource.SelectParameters.Add("SendDate", SendDate);
            ReportDataSource.SelectParameters.Add("UserOrgId", SessParam.OrganizationId.ToString());
            ReportDataSource.SelectParameters.Add("IsClient", IsClientHiddenField.Value);

            if (ContractorWON.Length > 0)
                ReportDataSource.SelectParameters.Add("ContractorWorkOrderNumber", ContractorWON);
            if (CustomerWON.Length > 0)
                ReportDataSource.SelectParameters.Add("CustomerWorkOrderNumber", CustomerWON);

            ReportDataSource.SelectParameters.Add("OrganizationId", ddlOrgs.SelectedValue);

            ReportGridview.DataSourceID = "ReportDataSource";

            ReportDataSource.DataBind();
            ReportGridview.DataBind();
        }

        private void ddlOrgs_Load(bool IsClient)
        {   
            ddlOrgs.Items.Clear();
            ListItem all = new ListItem("All", "0");
            ddlOrgs.Items.Add(all);
            ddlOrgs.AppendDataBoundItems = true;
            if (IsClient)
            {
                OrganizationsTitleLabel.Text = "Please choose contractor:";
                ddlOrgs.DataSource = MLY.Bll.clsOrganization.GetContractorsByClientId((int)Session["OrganizationId"]);
                ddlOrgs.DataBind();
            }
            else
            {
                OrganizationsTitleLabel.Text = "Please choose client:"; 
                ddlOrgs.DataSource = MLY.Bll.clsOrganization.GetClientsByContractorId((int)Session["OrganizationId"]);
                ddlOrgs.DataBind();
            }
            
        }

        protected void ExportToExcel(object sender, EventArgs e)
        {
            MLY.Bll.ExcelExport.PrepareCsvFileFromGridview(ReportGridview, "DeadInventoryReport");
        }
    }
}
